package tgc.edu.tms.web.controller;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.custom.TablePageable;
import tgc.edu.tms.entity.Profession;
import tgc.edu.tms.entity.SysDept;
import tgc.edu.tms.service.ProfessionService;
import tgc.edu.tms.service.SysDeptService;
import tgc.edu.tms.web.form.ProfessionForm;

@Controller
@RequestMapping(value="/profession")
public class ProfessionController extends CommonController<Profession,Integer, ProfessionForm>{

	@Autowired
	private ProfessionService professionService;
	@Autowired
	private SysDeptService deptService;
	
	@Override
	public Object save(ProfessionForm form) {
		try {
			Profession model=new Profession();
			Integer id = form.getId();
			if(id!=null) {	//修改
				model=professionService.findById(id);
				//如果修改后的数据和数据库内一致
				Profession findById = professionService.findById(id);
				if((findById.getCode())!=null) {
					if(findById.getYear().equals(form.getYear()) && findById.getCollege().equals(form.getCollege()) && findById.getProfesion().equals(form.getProfesion()) && findById.getCode().equals(form.getCode())) {
						return new AjaxResult("ALSO");
					}					
				}
			}
			//验证数据库中是否有 新增/修改 的相同字段
			Profession findByYearAndCollegeAndProfesion = professionService.findByYearAndCollegeAndProfesion(form.getYear(),form.getCollege(),form.getProfesion());
			Profession findByYearAndCollegeAndProfesionAndCode = professionService.findByYearAndCollegeAndProfesionAndCode(form.getYear(),form.getCollege(),form.getProfesion(),form.getCode());
			if(findByYearAndCollegeAndProfesionAndCode!=null && findByYearAndCollegeAndProfesion !=null ) {
				return new AjaxResult("该专业已存在！保存失败！");
			}
			BeanUtils.copyProperties(form, model,"id");
			professionService.save(model);
			return new AjaxResult("OK");
		}catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	@Override
	public Object page(TablePageable pageParam, ProfessionForm form) {
		pageParam.setSort("year");
		pageParam.setOrder("desc");
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<Profession> spec = buildSpec(form);
		Page<Profession> page=professionService.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
	}

	@Override
	public Specification<Profession> buildSpec(ProfessionForm form) {
		HashSet<Rule> rules=new HashSet<>();
		String year = form.getYear();
		if(StringUtils.hasText(year)) {
			rules.add(Rule.property("year").contain(year));
		}
		String college = form.getCollege();
		if(StringUtils.hasText(college)) {
			rules.add(Rule.property("college").contain(college));
		}
		String profesion = form.getProfesion();
		if(StringUtils.hasText(profesion)) {
			rules.add(Rule.property("profesion").contain(profesion));
		}
		String search = form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("year").contain(search));
			rules2.add(Rule.property("college").contain(search));
			rules2.add(Rule.property("profesion").contain(search));
			rules.add(Rule.or(rules2));
		} 
		return Rule.and(rules).toSpecification();
	}

	@Override
	public Object delete(Integer id) {
		try {
			professionService.deleteById(id);
			return new AjaxResult("数据删除成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据删除失败,请先删除该专业的教务安排!");
		}
	}
	
	
	@RequestMapping(value="/foundCollegeInput")
	@ResponseBody
	public Object fci() {
		List<SysDept> findByParent_id = deptService.findByParent_id(1);
		return findByParent_id;
	}
	
	@RequestMapping(value="/newYear")
	@ResponseBody
	public Object newYear(String year,String college) {
		//取出数据库里最近的一年
		List<String> findAllYear = professionService.findAllYear();
		String max = Collections.max(findAllYear);
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx"+max);
		//1寻找上一年该学院下的所有专业
		List<Profession> findByCollegeAndYear = professionService.findByCollegeAndYear(college,max);
		//如果该学院下的专业不为空
		if(findByCollegeAndYear.size()==0) {
			return "该学院下没有任何专业！请手动添加后再重试！";
		}else {
			//在用户输入的年份下依次添加专业
			for(Profession f:findByCollegeAndYear) {
				professionService.insert(year,college,f.getProfesion());
			}
			}
		return "添加成功！";
	}
}
